package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl implements ProductDao {
    @Override
    public int add(ProductModel productModel) {
        String sql = "INSERT INTO `product` (`name`, `normal_price`, `price`, \n" +
                "`content`, `is_show`, `is_recommend`, \n" +
                "`img_href`, `create_time`, `update_time`, \n" +
                "`remark`, `enable`, `where_show`, \n" +
                "`cuisine_id`, `style`) VALUES (\n" +
                "'" + productModel.getName() + "', '" + productModel.getNormalPrice() + "', '" + productModel.getPrice() + "', \n" +
                "'" + productModel.getContent() + "', '" + productModel.getIsShow() + "', '" + productModel.getIsRecommend() + "', \n" +
                "'" + productModel.getImgHref() + "', now(), now(), \n" +
                "'1', '1', '1',\n" +
                " '" + productModel.getCuisineId() + "', '1');";
        return JDBC.update(sql);
    }

    @Override
    public List<ProductModel> findAll(ProductModel productModel) {
        int page = (productModel.getPage() - 1) * productModel.getLimit();
        int limit = productModel.getLimit();
        String sql = "SELECT p.*,c.name cuisineName FROM product p\n" +
                "left join cuisine c\n" +
                "on p.cuisine_id=c.id ";
        sql += " order by p.id desc";
        sql += " limit " + page + "," + limit;
        List<ProductModel> list = new ArrayList<>();
        ResultSet resultSet = JDBC.query(sql);
        try {
            while (resultSet.next()) {
                ProductModel model = new ProductModel();
                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setNormalPrice(resultSet.getBigDecimal("normal_price"));
                model.setPrice(resultSet.getBigDecimal("price"));
                model.setImgHref(resultSet.getString("img_href"));
                model.setIsShow(resultSet.getInt("is_show"));
                model.setCuisineName(resultSet.getString("cuisineName"));
                model.setContent(resultSet.getString("content"));
                model.setCuisineId(resultSet.getInt("cuisine_id"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int update(ProductModel productModel) {
        String sql = "";
        return JDBC.update(sql);
    }

    @Override
    public ProductModel findById(ProductModel productModel) {

        String sql = "SELECT p.*,c.name cuisineName FROM product p\n" +
                "left join cuisine c\n" +
                "on p.cuisine_id=c.id and p.id=" + productModel.getId();
        ResultSet resultSet = JDBC.query(sql);
        ProductModel model = new ProductModel();
        try {
            while (resultSet.next()) {

                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setNormalPrice(resultSet.getBigDecimal("normal_price"));
                model.setPrice(resultSet.getBigDecimal("price"));
                model.setImgHref(resultSet.getString("img_href"));
                model.setIsShow(resultSet.getInt("is_show"));
                model.setCuisineName(resultSet.getString("cuisineName"));
                model.setContent(resultSet.getString("content"));
                model.setCuisineId(resultSet.getInt("cuisine_id"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public int delete(ProductModel productModel) {
        String sql = "delete from product where id=" + productModel.getId();
        return JDBC.update(sql);
    }

    @Override
    public int getCount(ProductModel productModel) {
        String sql = "SELECT count(*) count FROM product p\n" +
                "left join cuisine c\n" +
                "on p.cuisine_id=c.id";
        ResultSet resultSet = JDBC.query(sql);
        int count = 0;
        try {
            while (resultSet.next()) {
                count = resultSet.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
}
